<template>
  <div class="home-container">
    <el-aside class="aside" :width="isCollapse ? '64px' : '200px'">
      <!-- width属性使用v-bind指令（简写为冒号）动态绑定宽度，取决于isCollapse变量的值。 -->
      <div class="toggle-button" @click="toggleCollapse">|||</div>
      <el-menu :collapse="isCollapse" :router="true" :default-active="activePath" background-color="#304156"
        text-color="#bfcbd9" active-text-color="#409eff">
        <el-menu-item index="/welcome" class="menu-item">
          <i class="el-icon-s-home"></i>
          <span slot="title">首页</span>
        </el-menu-item>
        <el-submenu v-for="(menu, menuIndex) in menuList" :key="menu.id" :index="menu.id + ''">
          <!-- 使用v-for指令循环渲染menuList数组中的每个对象，创建子菜单 -->
          <template slot="title">
            <i :class="icons[menuIndex]"></i>
            <span>{{ menu.authName }}</span>
          </template>
          <el-menu-item v-for="subMenu in menu.children" :key="subMenu.id" :index="subMenu.path"
            @click="saveNavState(subMenu.path)" class="menu-item">
            <i class="el-icon-menu"></i>
            <span>{{ subMenu.authName }}</span>
          </el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header class="header">
        <el-button type="info" class="logout-button" @click="logout">退出</el-button>
      </el-header>
      <el-main class="main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      // 定义了组件的响应式数据
      isCollapse: false,
      activePath: '',
      menuList: [
        {
          id: 1,
          authName: '个人信息',
          children: [
            {
              id: '1-1',
              authName: '查询个人信息',
              path: '/personal/personalview'
            }
          ]
        },
        {
          id: 2,
          authName: '演唱会管理',
          children: [
            {
              id: '2-1',
              authName: '发布演唱会',
              path: '/Live/LivePublish'
            }
          ]
        },
        {
          id: 3,
          authName: '用户管理',
          children: [
            {
              id: '3-1',
              authName: '查询用户信息',
              path: '/User'
            }
          ]
        },
        {
          id: 4,
          authName: '票务管理',
          children: [
            {
              id: '4-1',
              authName: '查看票务',
              path: '/tickets/view'
            },
            {
              id: '4-2',
              authName: '添加票务',
              path: '/tickets/add'
            }
          ]
        },
        {
          id: 5,
          authName: '订单管理',
          children: [
            {
              id: '5-1',
              authName: '查看订单',
              path: '/orders/view'
            },
            {
              id: '5-2',
              authName: '处理订单',
              path: '/orders/process'
            }
          ]
        },
        {
          id: 6,
          authName: '通知管理',
          children: [
            {
              id: '6-1',
              authName: '发布通知',
              path: '/Information/Information'
            }
          ]
        },
        {
          id: 7,
          authName: '艺术家管理',
          children: [
            {
              id: '7-1',
              authName: '查看艺术家信息',
              path: '/Artist/ArtistView'
            }
          ]
        },
        {
          id: 8,
          authName: '讨论留言管理',
          children: [
            {
              id: '8-1',
              authName: '发布留言',
              path: '/Message/MessageAdd'
            },
          ]
        },
      ],
      icons: ['el-icon-s-custom', 'el-icon-microphone', 'el-icon-user-solid', 'el-icon-s-ticket', 'el-icon-s-order', 'el-icon-s-promotion', 'el-icon-tickets', 'el-icon-bell']
    };
  },
  created() {
    let activePath = sessionStorage.getItem('activePath');
    this.activePath = activePath || '/welcome';
  },
  methods: {
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    saveNavState(activePath) {
      sessionStorage.setItem('activePath', activePath);
      this.activePath = activePath;
    },
    logout() {
      localStorage.removeItem('currentUser');
      this.$router.push('/login');
    }
  }
};
</script>

<style scoped>
.home-container {
  display: flex;
  height: 100vh;
}

.aside {
  transition: width 0.3s;
  overflow: hidden;
}

.toggle-button {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 60px;
  width: 100%;
  background-color: #b3c0d1;
  color: #fff;
  cursor: pointer;
  user-select: none;
  font-size: 20px;
  line-height: 1;
}

.el-menu {
  border-right: none;
}

.menu-item {
  transition: background-color 0.3s;
}

.menu-item:hover {
  background-color: #263445;
}

.header {
  background-color: #304156;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 60px;
  margin: 0;
  padding: 0 20px;
  border-bottom: none;
}

.logout-button {
  margin: 0;
}

.main {
  padding: 20px;
  background-color: #fff;
  flex-grow: 1;
}
</style>
